package com.pdsu.graduation_design.views;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.pdsu.graduation_design.R;
import com.pdsu.graduation_design.domain.User;
import com.pdsu.graduation_design.listener.UserRegisterListener;
import com.pdsu.graduation_design.utils.JsonUtil;
import com.pdsu.graduation_design.utils.Result;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

public class UserRegisterActivity extends AppCompatActivity {

    private EditText username,name,password,phone,address,code;
    private CheckBox checkB;
    private TextView XIEYI;
    private Button submit;
    private Context context;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_register);
        init();
        bind();

    }
    void init(){
        username=findViewById(R.id.register_username_et);
        name=findViewById(R.id.register_name_et);
        password=findViewById(R.id.register_password_et);
        phone=findViewById(R.id.register_phone_et);
        address=findViewById(R.id.register_address_et);
        code=findViewById(R.id.register_code_et);
        checkB=findViewById(R.id.register_xieyi_ck);
        XIEYI=findViewById(R.id.register_xieyi);
        submit=findViewById(R.id.register_submit_btn);
        context=this;

    }
    void bind(){

        User user=new User();



        //

        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                user.setUsername(username.getText().toString());
                user.setName(name.getText().toString());
                user.setPhone(phone.getText().toString());
                user.setPassword(password.getText().toString());

                if (checkB.isChecked()){

                    if (user.getName().length()==0||user.getUsername().length()==0||user.getPhone().length()==0||user.getPassword().length()==0){
                        Toast.makeText(context, "请完善个人信息", Toast.LENGTH_SHORT).show();
                    }
                    else {
                        RequestParams requestParams=new RequestParams("http://10.0.2.2:8088/user/registerUser");
                        requestParams.setAsJsonContent(true);
                        String json_u = "";
                        try {
                             json_u= JsonUtil.ObjToJson(user);

                        } catch (JsonProcessingException e) {
                            e.printStackTrace();
                        }
                        requestParams.addParameter("user",json_u);
                        x.http().get(requestParams, new Callback.CommonCallback<String>() {
                            @Override
                            public void onSuccess(String result) {
                                Toast.makeText(context,result, Toast.LENGTH_SHORT).show();
                                try {
                                    Result r=JsonUtil.JsonToObj(result,Result.class);
                                    if (r.getCode().equals("200")){
                                        Toast.makeText(context, "注册成功", Toast.LENGTH_SHORT).show();
                                        finish();
                                    }else{
                                        Toast.makeText(context, "手机号已被占用，请换个手机号重试", Toast.LENGTH_SHORT).show();

                                    }

                                } catch (JsonProcessingException e) {
                                    e.printStackTrace();
                                }
                            }

                            @Override
                            public void onError(Throwable ex, boolean isOnCallback) {

                                System.out.println(ex);

                            }

                            @Override
                            public void onCancelled(CancelledException cex) {

                                System.out.println(cex);
                            }

                            @Override
                            public void onFinished() {

                                System.out.println("fin");
                            }
                        });


                    }
                }
                else {
                    Toast.makeText(context, "请阅读并同意用户协议", Toast.LENGTH_SHORT).show();
                }

            }
        });

    }
}